//
//  SportsRun_ViewController.m
//  Bicycle
//
//  Created by dllo on 16/4/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SportsRun_ViewController.h"
#import "SportsMap_ViewController.h"
#import "SportSave_ViewController.h"
#import "arcView.h"
#import <CoreLocation/CoreLocation.h>

@interface SportsRun_ViewController ()<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *speed;
@property(nonatomic,strong)CLLocationManager *lM;
@property(nonatomic,strong)CLLocation *oldLocation;
@property(nonatomic,assign)float sum;
@property(nonatomic,assign)NSInteger time_min;
@property(nonatomic,assign)NSInteger time_sec;
@property(nonatomic,assign)NSInteger time_msec;
@property(nonnull,strong)NSTimer *timer;
@property(nonatomic,assign)CGFloat sumkcal;
@property(nonatomic,strong)NSString *filePath;
@property(nonatomic,strong)arcView *watchView;
@property(nonatomic,assign)int speedNum;
@end

@implementation SportsRun_ViewController

-(CLLocationManager *)lM{
    
    if (!_lM) {
        _lM = [[CLLocationManager alloc]init];
        _lM.delegate = self;
        _lM.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        [_lM requestAlwaysAuthorization];
        _lM.allowsBackgroundLocationUpdates = YES;
    }
    return _lM;
}
//更改状态栏颜色
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self lM];
    self.sum = 0.0;
    self.time_min = 0;
    self.time_sec = -1;
    self.time_msec = 0;
    [self.lM startUpdatingLocation];
//    [self RunTime];

    self.watchView = [[arcView alloc]initWithFrame:CGRectMake(0, 0, APPW, APPH)];
    self.watchView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.watchView];

}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    //    NSLog(@"定位到了");
    
    CLLocation *location = [locations lastObject];
    
    double distance = 0;
    
    if(self.oldLocation)
    {
        distance = [location distanceFromLocation:self.oldLocation];
    }
//    NSLog(@"sfds  %f",distance);
    self.oldLocation = location;
    
    self.sum += distance;
//    NSLog(@"juli  %f",self.sum);
    NSLog(@"dfd%ld",self.time_sec);
    
    //路程
    self.runDistance.text = [NSString stringWithFormat:@"%.2f",self.sum / 1000];
    //实时速度
//    self.speed.text = [NSString stringWithFormat:@"%.2f", location.speed + 1];
    self.watchView.num = location.speed + 1 ;
    //卡路里
    CGFloat kcal = ((location.speed + 1) * 60 * 60 / 1000) * 70 * 1.5 / 60 / 60;
    
    self.sumkcal = self.sumkcal + kcal;
    
    self.runKcal.text = [NSString stringWithFormat:@"%.1f",self.sumkcal];
    
    self.time_sec++;
//    if (self.time_msec == 100) {
//        self.time_sec++;
//        self.time_msec = 0;
        if (self.time_sec == 60) {
            self.time_min++;
            self.time_sec = 0;
        }
//    }
    if (self.time_sec <= 0) {
        self.runTime.text = [NSString stringWithFormat:@"00:00"];
    }else{
        self.runTime.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)self.time_min,(long)self.time_sec];//保留两位 不够补零
    }
}

//-(void)RunTime{
//    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1/100.0 target:self selector:@selector(UserTimer) userInfo:nil repeats:YES];
//    [self.timer setFireDate:[NSDate distantPast]];
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
//}
//-(void)UserTimer{
//    self.time_msec++;
//    if (self.time_msec == 100) {
//        self.time_sec++;
//        self.time_msec = 0;
//        if (self.time_sec == 60) {
//            self.time_min++;
//            self.time_sec = 0;
//        }
//    }
//    self.runTime.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)self.time_min,(long)self.time_sec];//保留两位 不够补零
//    NSLog(@"%02ld:%02ld:%02ld",(long)self.time_min,(long)self.time_sec,(long)self.time_msec);
//    
//}

- (IBAction)Map:(id)sender {
    
    //地图单例
    SportsMap_ViewController *mapVC = [SportsMap_ViewController shareInstance];
    mapVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    mapVC.isStart = self.isStart;
    [self presentViewController:mapVC animated:YES completion:nil];

    
    
}
- (IBAction)Pause:(UIButton *)sender {
    
    if (self.isStart) {
        [sender setBackgroundImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
        //关闭定时器
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.lM stopUpdatingLocation];
        
    }else{
        self.oldLocation = nil;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
        //开启定时器
        [self.timer setFireDate:[NSDate distantPast]];
        [self.lM startUpdatingLocation];
        
    }
    self.isStart = !self.isStart;

    
}
- (IBAction)Stop:(id)sender {
    
    [self.lM stopUpdatingLocation];
    [self.timer invalidate];
    self.isStart = NO;
    [self.delegate passRunStart:self.isStart];
    SportSave_ViewController *saveVC = [[SportSave_ViewController alloc]init];
    //    NSLog(@"距离%@ %@ ",self.runDistance.text,self.runTime.text);
    saveVC.distance = self.sum;
    saveVC.time = [NSString stringWithFormat:@"%02ld:%02ld",(long)self.time_min,(long)self.time_sec];
    saveVC.averSpeed = [NSNumber numberWithFloat:self.sum / (self.time_min * 60 + self.time_sec)];
    saveVC.speed = [NSString stringWithFormat:@"%.2f",self.sum / (self.time_min * 60 + self.time_sec)];
    
    
    
    [self presentViewController:saveVC animated:YES completion:nil];

    
}
- (IBAction)BackToHome:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
